package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.LuntaiPolicyExtensionRequestMapper;
import com.ruoyi.system.mapper.LuntaiRescueApplicationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LuntaiOrderMapper;
import com.ruoyi.system.domain.LuntaiOrder;
import com.ruoyi.system.service.ILuntaiOrderService;

/**
 * 订单信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class LuntaiOrderServiceImpl implements ILuntaiOrderService {
    @Autowired
    private LuntaiOrderMapper luntaiOrderMapper;
    @Autowired
    private LuntaiPolicyExtensionRequestMapper luntaiPolicyExtensionRequestMapper;


    @Autowired
    private LuntaiRescueApplicationMapper luntaiRescueApplicationMapper;


    /**
     * 查询订单信息
     *
     * @param id 订单信息主键
     * @return 订单信息
     */
    @Override
    public LuntaiOrder selectLuntaiOrderById(Integer id) {
        return luntaiOrderMapper.selectLuntaiOrderById(id);
    }

    /**
     * 查询订单信息列表
     *
     * @param luntaiOrder 订单信息
     * @return 订单信息
     */
    @Override
    public List<LuntaiOrder> selectLuntaiOrderList(LuntaiOrder luntaiOrder) {
        return luntaiOrderMapper.selectLuntaiOrderList(luntaiOrder);
    }

    /**
     * 新增订单信息
     *
     * @param luntaiOrder 订单信息
     * @return 结果
     */
    @Override
    public int insertLuntaiOrder(LuntaiOrder luntaiOrder) {
        luntaiOrder.setCreateTime(DateUtils.getNowDate());
        return luntaiOrderMapper.insertLuntaiOrder(luntaiOrder);
    }

    /**
     * 修改订单信息
     *
     * @param luntaiOrder 订单信息
     * @return 结果
     */
    @Override
    public int updateLuntaiOrder(LuntaiOrder luntaiOrder) {
        return luntaiOrderMapper.updateLuntaiOrder(luntaiOrder);
    }

    /**
     * 批量删除订单信息
     *
     * @param ids 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteLuntaiOrderByIds(Integer[] ids) {
        return luntaiOrderMapper.deleteLuntaiOrderByIds(ids);
    }

    /**
     * 删除订单信息信息
     *
     * @param id 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteLuntaiOrderById(Integer id) {
        return luntaiOrderMapper.deleteLuntaiOrderById(id);
    }

    @Override
    public int finish(LuntaiOrder luntaiOrder) {
        LuntaiOrder order = luntaiOrderMapper.selectLuntaiOrderById(luntaiOrder.getId());
        Integer type = order.getType();
        if (type < 3) {
            //鼓包/换胎的申请订单
            finishRequest(order);
        } else if (type == 3) {
            //救援订单
            finishApplication(order);
        }
        luntaiOrder.setAdminId(Math.toIntExact(SecurityUtils.getUserId()));
       return luntaiOrderMapper.updateLuntaiOrder(luntaiOrder);
    }


    public void finishRequest(LuntaiOrder order) {
        Integer id = order.getSourceId();
        luntaiPolicyExtensionRequestMapper.finishRequest(id);
        System.out.println("鼓包申请已完成，requestId为"+id);
    }

    public void finishApplication(LuntaiOrder order) {
        Integer id = order.getSourceId();
        luntaiRescueApplicationMapper.finishRequest(id);
    }
}
